package com.party.mobile.utils;

import com.alibaba.fastjson.JSONObject;
import com.party.common.utils.AESUtil;
import com.party.mobile.web.dto.login.output.CurrentUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * 登陆权限管理工具类
 * party
 * Created by wei.li
 * on 2016/10/24 0024.
 */
public class RealmUtils {

    public static final int HASH_INTERATIONS = 1024;
    public static final int SALT_SIZE = 8;

    /**
     * 登陆用户转token
     *
     * @param currentUser 登陆用户
     * @return token值
     */
    public static String getToken(CurrentUser currentUser) {
        String token = JSONObject.toJSONString(currentUser);
        token = AESUtil.encrypt(token, currentUser.getId());
        return token;
    }


    /**
     * 获取session
     *
     * @return
     */
    public static Session getSession() {
        try {
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession(false);
            if (session == null) {
                session = subject.getSession();
            }
            if (session != null) {
                return session;
            }
        } catch (InvalidSessionException e) {

        }
        return null;
    }
}
